<template>
    <div>
        <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
            <el-breadcrumb-item :to="{ path: '/ManageHome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>聊天应用</el-breadcrumb-item>
            <el-breadcrumb-item>应用管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
            <div>
                <!--    功能区-->
                <div style="margin: 10px 0">
                    <!--    搜索区-->
                    <div style="margin: 10px 0">
                        <span
                            style="margin-left: 40px; margin-right: 14px; font-size: 15px; font-weight: 700;">应用名称</span>
                        <el-input v-model="search" size="small" clearable placeholder="应用名称"
                            prefix-icon="el-icon-search" style="width: 15%;" />
                        <el-button class="mybutton " icon="el-icon-search" @click="load"
                            style="background-color: #143371; color: aliceblue;" size="small">搜索</el-button>
                        <el-button class="mybutton " icon="el-icon-refresh-left" style="margin-left: 5px;"
                            @click="reset" size="small">重置</el-button>
                        <div style="float: right">
                            <el-tooltip content="添加应用" placement="top">
                                <el-button class="mybutton " style="background-color: #143371; color: aliceblue;"
                                    size="small" @click="add">+新增</el-button>
                            </el-tooltip>
                        </div>
                    </div>
                </div>
                <div class="card-list" style="display: flex;flex-wrap: wrap;">
                    <div v-for="(item, index) in tableData" :key="index" @click="toAppDetail(item.id)"
                        style="width: 24%; margin-top: 15px;">
                        <el-card style="margin-left: 20px;">
                            <div style="display: flex;justify-content: space-between;">
                                <div style="display: flex; width: 80%;">
                                    <div style="background-color:rgb(219 234 254); width: 70px; height: 70px; border-radius: 5px;"
                                        class="img-container">
                                        <img src="@/assets/image/AI.png" alt="" width="40%" style="margin-top: 1px;">
                                    </div>
                                    <div style="margin-left: 10px; width: 50%;">
                                        <p style="font-weight: bold;">{{ item.name }}</p>
                                        <p
                                            style="font-size: 12px; margin-top: 5px; color: gray;overflow: hidden;   text-overflow: ellipsis ">
                                            {{ item.des }}</p>
                                    </div>
                                </div>
                                <div style="width: 15%;">
                                    <i @click="handleEdit(scope.$index, scope.row)" class="el-icon-edit"
                                        style="margin-right: 5px;"></i>
                                    <i @click="handleDelete(scope.$index, scope.row)" class="el-icon-delete"></i>
                                </div>
                            </div>
                            <div style="color: gray; font-size: 12px; margin-top: 10px;">
                                <i class="el-icon-full-screen">{{ item.modelName }}</i>
                                <i class="el-icon-folder" style="margin-left: 20px;">知识库数:{{ item.docCount }}</i>
                            </div>
                        </el-card>

                    </div>
                </div>
                <!--分页-->
                <div style="margin: 10px 0">
                    <el-pagination :currentPage="currentPage" :page-size="pageSize" :page-sizes="[10, 20]"
                        :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>

                <div v-loading="dialogloading">
                    <!--  弹窗-->
                    <el-dialog :visible.sync="dialogVisible" title="操作" width="50%" @close="cancel" top="30px">
                        <div v-if="isDetail == 0">
                            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                                <el-form-item label="应用名称" prop="name">
                                    <el-input v-model="form.name" style="width: 80%" placeholder="名称"></el-input>
                                </el-form-item>
                                <el-form-item label="关联的模型" prop="modelId">
                                    <el-select v-model="form.modelId" placeholder="请选择模型">
                                        <el-option style="margin-left: 12px;"  v-for="(item, index) in allModels" :key=index :label=item.name
                                            :value=item.id></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="封面图" prop="cover">
                                    <el-upload action="" list-type="picture-card" :limit="1"
                                        :on-preview="handlePictureCoverPreview" :on-remove="handleCoverRemove"
                                        accept=".jpg,.png" :on-exceed="handleExceed" :file-list="cover_list"
                                        :http-request="UploadCover">
                                        <i class="el-icon-plus"></i>
                                    </el-upload>
                                    <el-dialog :visible.sync="CoverdialogVisible">
                                        <img width="100%" :src="CoverdialogImageUrl" alt="">
                                    </el-dialog>
                                </el-form-item>
                                <el-form-item label="应用描述" prop="des">
                                    <el-input type="textarea" v-model="form.des" style="width: 80%"
                                        placeholder="描述"></el-input>
                                </el-form-item>
                            </el-form>
                        </div>
                        <template #footer>
                            <span class="dialog-footer" v-if="isDetail == 0">
                                <el-button @click="cancel">取 消</el-button>
                                <el-button type="primary" @click="save">确 定</el-button>
                            </span>
                        </template>
                    </el-dialog>
                </div>
            </div>
        </el-card>
    </div>
</template>
<script>
import request from '@/utils/request'
import { Upload } from 'element-ui';
export default {
    name: "ModelManage",
    data() {
        return {
            color: '#000000',
            dialogloading: false,
            isDetail: 0,
            value: null,
            loading: true,
            editJudge: true,
            judge: false,
            dialogVisible: false,
            search: null,
            currentPage: 1,
            pageSize: 10,
            total: 0,
            mode: 'default',
            allModels: [{
                name: "千帆大模型",
                id: "1232"
            },
            {
                name: "零一大模型",
                id: "123222"
            }],
            tableData: [
                {
                    id: "1232421badad",
                    modelId: "关联的模型Id",
                    modelName: "大模型",
                    cover: "封面",
                    name: "客服助手",
                    prompt: "提示词",
                    knowledgeIds: [],
                    docCount: 2,
                    totalSize: 12042,
                    des: "关于客服助手灌输式达到啊啊的八点"
                },

            ],
            form: {
                id: "1232421badad",
                modelId: "关联的模型Id",
                modelName: "关联的模型名称",
                cover: "封面",
                name: "客服助手",
                prompt: "提示词",
                knowledgeIds: [],
                docCount: 2,
                totalSize: 12042,
                des: "关于客服助手灌输式达到啊啊的八点"
            },
            rules: {
            },
            editDisplay: {
                display: "block",
            },
            display: {
                display: "none",
            },
            cover_list: [],
            CoverdialogVisible: false,
            CoverdialogImageUrl: "",

        };
    },
    created() {
        this.load();
        this.loading = false;
        setTimeout(() => {
            //设置延迟执行
            this.loading = false;
        }, 1000);
    },
    methods: {
        parseSizeToMb(num) {
            num = num / (1024 * 1024)
            return parseFloat(num.toFixed(2))
        },

        async load() {

        },
        reset() {
            this.search = null
            this.value = null,
                this.currentPage = 1;
            this.searchJob = null
            this.pageSize = 10
            this.load()
        },
        detail(index, row) {
            this.isDetail = 1;
            this.dialogVisible = true;
            // 赋值数据
            this.$nextTick(() => {
                this.form = row
            });
        },
        handleDelete(index, row) {
            // 确认框确认是否要删除
            this.$confirm("确定要删除的信息吗?", "删除提示", {
                iconClass: "el-icon-question", //自定义图标样式
                confirmButtonText: "残忍删除", //确认按钮文字
                cancelButtonText: "取消删除", //取消按钮文字
                showClose: true, //是否显示右上角关闭按钮
                type: "warning", //提示类型  success/info/warning/error
            }).then(() => {  //选择确认按钮进入此方法

            }).catch(() => { //选择取消按钮进入此方法
                //取消操作

            });
        },
        handleEdit(index, row) {
            this.judge = true;
            this.isDetail = 0;
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.form = row
            });
        },
        cancel() {
            this.form = {}
            this.dialogVisible = false
        },
        save() {
            this.$refs.form.validate(vaild => {
                if (vaild) {
                    console.log(1111)
                    if (this.judge) {

                    }
                    else {
                        const loading = this.$loading({
                            lock: true,
                            text: '添加中',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });

                    }
                }
            })
        },
        formatDate(chinaTime) {
            const year = chinaTime.getFullYear();
            const month = (chinaTime.getMonth() + 1).toString().padStart(2, '0');
            const day = chinaTime.getDate().toString().padStart(2, '0');
            const hours = chinaTime.getHours().toString().padStart(2, '0');
            const minutes = chinaTime.getMinutes().toString().padStart(2, '0');
            const seconds = chinaTime.getSeconds().toString().padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        add() {
            this.dialogVisible = true;
            this.isDetail = 0;
            this.judge = false;
            this.form = {}
        },
        handleSizeChange(pageSize) {
            //改变每页个数
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {
            //改变页码
            this.currentPage = pageNum;
            this.load();
        },
        toAppDetail(id) {
            this.$router.push({
                path: "/AppConfig",
                query: {
                    id: id
                }
            })
        },
        handleCoverRemove(file, fileList) {
            console.log("删除", file);
            this.cover_list = []
        },
        handlePictureCoverPreview(file) {
            this.CoverdialogImageUrl = file.url;
            this.CoverdialogVisible = true;
        },
        handleExceed() {
            this.$message.warning(`只允许上传一张图片`);
        },
        UploadCover(data) {
            // var formData = new FormData()
            // formData.append('type', 'view')
            // formData.append('file', data.file)
            this.dialogloading = true;

            this.dialogloading = false;
            // console.log("图片文件", data)
            // console.log("现在的图片", this.cover_list)
            // request.post("/scenery/upload", formData).then(res => {
            //     if (res.code === '1') {
            //         this.cover_list = []
            //         this.cover_list.push({ name: data.file.name, url: res.data })
            //         this.dialogloading = false;
            //     } else {
            //         this.dialogloading = false
            //         this.$message({
            //             type: 'error',
            //             message: '上传失败'
            //         })
            //     }

            // })
        },
    }


}
</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* 防止边框和内边距影响宽度 */
}

.el-form-item {
    margin: 1px !important;
    padding: 8px !important;
}

.el-button {
    margin: 5px !important;
    padding: 8px !important;
}

.img-container {
    display: flex;
    /* 使用 flex 布局 */
    justify-content: center;
    /* 水平居中 */
    align-items: center;
    /* 垂直居中 */


}

.demo-table-expand {
    font-size: 0;
}

.demo-table-expand label {
    width: 150px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 90%;
    display: block;
}

.demo-table-expand .el-form-item p {
    height: 30px;
    padding: 0px 0px;
    margin-bottom: 10px;
    margin-top: 10px;
}

.el-upload-list__item {
    transition: none !important;
}
</style>